package com.xiaodimall.backsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodimall.backsys.common.JsonResult;
import com.xiaodimall.backsys.common.PageResult;
import com.xiaodimall.backsys.mapper.GoodsInfoMapper;
import com.xiaodimall.backsys.pojo.GoodsInfo;
import com.xiaodimall.backsys.service.GoodsInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author:
 * @Date: Created in 2022/11/25 20:26
 * @Description:
 */
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {


    /**
     * @Author: CJJ
     * @Description: 商品信息持久层对象
     */
    @Resource
    private GoodsInfoMapper goodsInfoMapper;


    /**
     * @param currentPage 当前页
     * @param pageSize    每页显示大小
     * @return 自定义类型, 封装总记录数和当前查询到的记录数
     * @Author: CJJ
     * @Description: 分页查询商品信息
     */
    @Override
    public PageResult<GoodsInfo> getAllGoodsInfoByPage(String currentPage, String pageSize) {

        //1 创建分页对象
        Page<GoodsInfo> page = new Page<>();

        //2 设置当前页
        page.setCurrent(Long.parseLong(currentPage));

        //3 设置最大显示数
        page.setSize(Long.parseLong(pageSize));

        //4 调用mp分页查询方法
        Page<GoodsInfo> goodsInfoPage = goodsInfoMapper.selectPage(page, new QueryWrapper<>());

        //5 获取查询到的记录
        List<GoodsInfo> records = goodsInfoPage.getRecords();

        //6 获取总记录数
        long total = goodsInfoPage.getTotal();

        //7 将结果封装到对象中并返回
        return new PageResult<GoodsInfo>(total, records);
    }


    /**
     * @Author: CJJ
     * @param goodsInfo 商品实体
     * @return JR
     * @Description: 完成编辑商品相关业务
     */
    @Override
    public JsonResult updateGoodsInfo(GoodsInfo goodsInfo) {
        //1 处理数据
        //1.1 将变更时间改为现在
        goodsInfo.setUpdateTime(new Date());
        //1.2 将修改用户id改为当前登录用户

        //1.3 将图片改为原名
        String goodsCoverImg = goodsInfo.getGoodsCoverImg();
        String regex = "/goods-img/.*jpg";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(goodsCoverImg);
        while (matcher.find()) {
            String group = matcher.group(0);
            goodsInfo.setGoodsCoverImg(group);
        }
        //2 调用持久层
        int row = goodsInfoMapper.updateById(goodsInfo);

        //3 判断返回
        //3.1 重新查询该条数据, 判断当前操作是上架/下架
        Integer goodsSellStatus = goodsInfoMapper.selectById(goodsInfo.getGoodsId()).getGoodsSellStatus();
        if (row != 1) {
            return new JsonResult(201, "修改失败,服务器错误");
        } else {
            if (goodsSellStatus == 1) {
                return new JsonResult(200, "down");
            } else {
                return new JsonResult(200, "up");
            }
        }
    }


    /**
     * @Author: CJJ
     * @param goodsInfo 商品信息实体
     * @return JR
     * @Description: 添加商品
     */
    @Override
    public JsonResult addGoodsService(GoodsInfo goodsInfo) {
        //1 处理数据
        //1.1 赋默认值
        if (goodsInfo.getGoodsDetailContent() == null || "".equals(goodsInfo.getGoodsDetailContent())) {
            goodsInfo.setGoodsDetailContent("<p>商品介绍加载中...</p>");
        }

        //1.2 根据商品名和商品描述查询该商品是否重复
        GoodsInfo goodsInfo1 = goodsInfoMapper.selectOne(
                new QueryWrapper<GoodsInfo>()
                        .eq("goods_name", goodsInfo.getGoodsName())
                        .eq("goods_intro", goodsInfo.getGoodsIntro()));
        if (goodsInfo1 != null) {
            return new JsonResult(202, "该商品已存在");
        }
        int row = goodsInfoMapper.insert(goodsInfo);
        if (row != 1) {
            return new JsonResult(201, "添加失败,服务器错误");
        }
        return new JsonResult(200, "添加成功");
    }



    /**
     * @Author: CJJ
     * @param originalPrice 商品标价
     * @return 商城售价标记点
     * 根据传入的商品标价实时计算各个折扣标记点数值
     */
    @Override
    public Map<Integer, String> getMarks(Integer originalPrice) {
        HashMap<Integer, String> marks = new HashMap<>();
        marks.put(0,"骨折");
        marks.put((int) (originalPrice*0.1),"1折");
        marks.put((int) (originalPrice*0.2),"2折");
        marks.put((int) (originalPrice*0.3),"3折");
        marks.put((int) (originalPrice*0.4),"4折");
        marks.put((int) (originalPrice*0.5),"5折");
        marks.put((int) (originalPrice*0.6),"6折");
        marks.put((int) (originalPrice*0.7),"7折");
        marks.put((int) (originalPrice*0.8),"8折");
        marks.put((int) (originalPrice*0.9),"9折");
        marks.put(originalPrice,"");
        return marks;
    }


    /**
     * @Author: CJJ
     * @param goodsId 商品ID
     * @return JR
     * @Description: 删除单个商品
     */
    @Override
    public JsonResult deleteGoodsById(Integer goodsId) {
        int row = goodsInfoMapper.deleteById(goodsId);
        if (row != 1){
            return new JsonResult(201,"服务器错误");
        }
        return new JsonResult(200,"已将商品删除");
    }

    /**
     * @Author: CJJ
     * @param goodsIds 商品ID数组
     * @return JR
     * @Description: 批量删除商品
     */
    @Override
    public JsonResult deleteGoodsByIds(List<Integer> goodsIds) {
        int i = goodsInfoMapper.deleteBatchIds(goodsIds);
        if (i != goodsIds.size()){
            return new JsonResult(201,"服务器错误");
        }
        return new JsonResult(200,"删除成功");
    }

    /**
     * 商品搜索
     * @param column 要搜索的表字段
     * @param value 搜索的值
     * @return JR
     */
    @Override
    public JsonResult selectGoodsBuCondition(String column, String value) {

        // 条件构造器
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();

        // 去除数据两边空格
        column = column.trim();
        value = value.trim();

        // 如果搜索的是编号, 将值改为数字查找
        if ("goods_id".equals(column)){
            queryWrapper.like(column, Integer.parseInt(value));
            List<GoodsInfo> list = goodsInfoMapper.selectList(queryWrapper);
            return new JsonResult(200,"",list);
        }
        queryWrapper.like(column, value);
        List<GoodsInfo> list = goodsInfoMapper.selectList(queryWrapper);
        return new JsonResult(200,"",list);
    }

    @Override
    public JsonResult startSellSelectGoods(List<Integer> goodsIds) {
        goodsInfoMapper.updateGoodsSellStatusTo0ByIds(goodsIds);
        return new JsonResult(200,"");
    }

    @Override
    public JsonResult stopSellSelectGoods(List<Integer> goodsIds) {
        goodsInfoMapper.updateGoodsSellStatusTo1ByIds(goodsIds);
        return new JsonResult(200,"");

    }
}
